// 这个是开发测试文件
import task from '../lib/index'

// task.config.autoRemovefinished = false;

// task.on('add start end error progress', (type, data) => {
task.on('progress', (type, data) => {
    log(`progress: ${data.group.name}, ${(data.progress.percent * 100).toFixed(0)}%`);
});

// add default group task
for (let n = 1; n < 10; n++) {
    task.add((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, 1000);
    });
    //.then(res => console.log(`default task ${n} is resolve`))
}

// add group2 task
task.setGroup('group2', { allowTasks: 3, autoRun: false});

for (let n = 1; n < 10; n++) {
    task.add((resolve, reject) => {
        setTimeout(() => {
            // log('run group2 task' + n);
            resolve();
        }, 1200);
    }, 'group2');
}

// console.log(task.getGroups());

(document.getElementById('btn-stop-default') as Element).addEventListener('click', evt => {
    task.stop('default');
    log('❤ default is stop');
});

(document.getElementById('btn-start-default') as Element).addEventListener('click', evt => {
    task.start('default');
    log('💚 default is start');
});

(document.getElementById('btn-start-group2') as Element).addEventListener('click', evt => {
    task.start('group2');
    log('💚 group2 is start');
});

function log(str: string): void {
    let el:Element = document.getElementById('log') as Element;
    let p:Element = document.createElement('p') as Element;
    p.innerHTML = str;
    el.prepend(p);
}